﻿namespace DoNet.Rsa
{
    /// <summary>
    /// 密钥格式化类
    /// </summary>
    public class RsaPemFormatHelper
    {
        /// <summary>
        /// 格式化Pkcs1格式私钥
        /// </summary>
        /// <param name="pkcs1PrivateKey">Pkcs1格式私钥</param>
        /// <returns></returns>
        public static string Pkcs1PrivateKeyFormat(string pkcs1PrivateKey)
        {
            return PkcsPrivateKeyFormat(pkcs1PrivateKey, RsaKeyFormat.Pkcs1);
        }

        /// <summary>
        /// 删除Pkcs1格式私钥格式
        /// </summary>
        /// <param name="pkcs1PrivateKey">Pkcs1格式私钥</param>
        /// <returns></returns>
        public static string Pkcs1PrivateKeyFormatRemove(string pkcs1PrivateKey)
        {
            return PkcsPrivateKeyFormatRemove(pkcs1PrivateKey, RsaKeyFormat.Pkcs1);
        }

        /// <summary>
        /// 格式化Pkcs8格式私钥
        /// </summary>
        /// <param name="pkcs8PrivateKey">Pkcs8格式私钥</param>
        /// <returns></returns>
        public static string Pkcs8PrivateKeyFormat(string pkcs8PrivateKey)
        {
            return PkcsPrivateKeyFormat(pkcs8PrivateKey, RsaKeyFormat.Pkcs8);
        }

        /// <summary>
        /// 删除Pkcs8格式私钥格式
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string Pkcs8PrivateKeyFormatRemove(string pkcs8PrivateKey)
        {
            return PkcsPrivateKeyFormatRemove(pkcs8PrivateKey, RsaKeyFormat.Pkcs8);
        }

        /// <summary>
        /// 格式化Pkcs格式私钥
        /// </summary>
        /// <param name="pkcsPrivateKey">Pkcs格式私钥</param>
        /// <param name="pkcs">密钥格式 Pkcs1，Pkcs8</param>
        /// <returns></returns>
        public static string PkcsPrivateKeyFormat(string pkcsPrivateKey, RsaKeyFormat pkcs = RsaKeyFormat.Pkcs1)
        {
            if(pkcs == RsaKeyFormat.Pkcs1)
            {
                pkcsPrivateKey = Pkcs1PrivateKeyFormatRemove(pkcsPrivateKey);
            }
            else if (pkcs == RsaKeyFormat.Pkcs8)
            {
                pkcsPrivateKey = Pkcs8PrivateKeyFormatRemove(pkcsPrivateKey);
            }

            List<string> result = new List<string>();

            if (pkcs == RsaKeyFormat.Pkcs1)
            {
                result.Add("-----BEGIN RSA PRIVATE KEY-----");
            }
            else if (pkcs == RsaKeyFormat.Pkcs8)
            {
                result.Add("-----BEGIN PRIVATE KEY-----");
            }

            foreach (var key in KeyFormat(pkcsPrivateKey))
            {
                result.Add(key);
            }

            if (pkcs == RsaKeyFormat.Pkcs1)
            {
                result.Add("-----END RSA PRIVATE KEY-----");
            }
            else if (pkcs == RsaKeyFormat.Pkcs8)
            {
                result.Add("-----END PRIVATE KEY-----");
            }
            var resStr = string.Join(Environment.NewLine, result);
            return resStr;
        }

        /// <summary>
        /// 删除Pkcs格式私钥格式
        /// </summary>
        /// <param name="pkcs1PrivateKey">Pkcs格式私钥</param>
        /// <param name="pkcs">密钥格式 Pkcs1，Pkcs8</param>
        /// <returns></returns>
        public static string PkcsPrivateKeyFormatRemove(string pkcsPrivateKey, RsaKeyFormat pkcs = RsaKeyFormat.Pkcs1)
        {
            string privateKey = string.Empty;
            if (pkcs == RsaKeyFormat.Pkcs1)
            {
                privateKey = pkcsPrivateKey.Replace("-----BEGIN RSA PRIVATE KEY-----", "").Replace("-----END RSA PRIVATE KEY-----", "").Replace(Environment.NewLine, "");
            }
            else if (pkcs == RsaKeyFormat.Pkcs8)
            {
                privateKey = pkcsPrivateKey.Replace("-----BEGIN PRIVATE KEY-----", "").Replace("-----END PRIVATE KEY-----", "").Replace(Environment.NewLine, "");
            }
            return privateKey;
        }

        /// <summary>
        /// 格式化公钥
        /// </summary>
        /// <param name="publicKey">公钥</param>
        /// <returns></returns>
        public static string PublicKeyFormat(string publicKey)
        {
            publicKey = PublicKeyFormatRemove(publicKey);

            List<string> result = new List<string>();
            result.Add("-----BEGIN PUBLIC KEY-----");
            foreach(var key in KeyFormat(publicKey))
            {
                result.Add(key);
            }
            result.Add("-----END PUBLIC KEY-----");
            return string.Join(Environment.NewLine, result);
        }

        /// <summary>
        /// 删除公钥格式
        /// </summary>
        /// <param name="publicKey">公钥</param>
        /// <returns></returns>
        public static string PublicKeyFormatRemove(string publicKey)
        {
            return publicKey.Replace("-----BEGIN PUBLIC KEY-----", "").Replace("-----END PUBLIC KEY-----", "").Replace(Environment.NewLine, "");
        }

        /// <summary>
        /// key格式化
        /// </summary>
        /// <param name="key">不带某某开头和某某结尾的key</param>
        /// <returns></returns>
        public static List<string> KeyFormat(string key)
        {
            List<string> res = new List<string>();
            int pos = 0;
            while (pos < key.Length)
            {
                var count = key.Length - pos < 64 ? key.Length - pos : 64;
                res.Add(key.Substring(pos, count));
                pos += count;
            }
            return res;
        }
    }
}
